package view.admin.equipManage;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXTextField;
import dao.EquipMsgDao;
import dao.ProductMsgDao;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.equip.EquipMsgEntity;
import pojo.equip.EquipMsgProperty;
import pojo.product.ProductMsgEntity;
import pojo.product.ProductMsgProperty;
import pojo.user.UserEntity;
import view.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class EquipMsgManageController {
    private Main myApp;
    private static List<EquipMsgEntity> equipMsgEntities = new ArrayList<>() ;
    private static EquipMsgDao equipMsgDao = new EquipMsgDao();

    public String getId() throws IOException {
        return equipMsgEntities.get(0).getId();
    }
    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public EquipMsgManageController(){}

    @FXML
    private JFXTextField searchName;

    @FXML
    private TableView<EquipMsgProperty> equipTable;

    @FXML
    private TableColumn<EquipMsgProperty, Void> option;

    @FXML
    private TableColumn<EquipMsgProperty, String> id;

    @FXML
    private TableColumn<EquipMsgProperty, String> code;

    @FXML
    private TableColumn<EquipMsgProperty, String> name;

    @FXML
    private TableColumn<EquipMsgProperty, String> type;

    @FXML
    private TableColumn<EquipMsgProperty, String> mode;

    @FXML
    private TableColumn<EquipMsgProperty, String> detail;

    @FXML
    private TableColumn<EquipMsgProperty, String> equipState;

    @FXML
    private TableColumn<EquipMsgProperty, String> rentState;

    @FXML
    private TableColumn<EquipMsgProperty, String> factory;

    @ FXML
    void initialize() throws IOException {
        EquipMsgDao equipMsgDao = new EquipMsgDao();
        this.displayInfo(equipMsgDao.findMsgList());
    }

    @FXML
    void changeEquip(ActionEvent event) throws Exception {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if(equipMsgEntities.size()>1) {
            alert.setHeaderText("!");
            alert.setContentText("只能选择一个哦");
            alert.showAndWait();
            equipMsgEntities = new ArrayList<>();
            this.initialize();
        }else if(equipMsgEntities.size() == 0){
            alert.setHeaderText("!");
            alert.setContentText("至少选择一个");
            alert.showAndWait();
            equipMsgEntities = new ArrayList<>();
            this.initialize();

        }
        else {
            myApp.goToChangeEquipPage();
            equipMsgEntities = new ArrayList<>();
            this.initialize();
        }
        equipMsgEntities = new ArrayList<>() ;
    }

    @FXML
    void changeEquipState(ActionEvent event) throws IOException {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if(equipMsgEntities.size() == 0){
            alert.setHeaderText("!");
            alert.setContentText("至少选择一个");
            alert.showAndWait();
            equipMsgEntities = new ArrayList<>();
        } else {
            for(EquipMsgEntity equip : equipMsgEntities) {
                equipMsgDao.changeEquipState(equip);
            }
        }
        this.displayInfo(equipMsgDao.findMsgList());
        equipMsgEntities = new ArrayList<>() ;

    }

    @FXML
    void deletEquip(ActionEvent event) throws IOException {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        equipTable.setItems(FXCollections.emptyObservableList());
        EquipMsgDao equipMsgDao = new EquipMsgDao();
        for(EquipMsgEntity equipMsgEntity:equipMsgEntities){
            if(equipMsgEntity.getRentState().equals("工厂设备")){
                alert.setHeaderText("!");
                alert.setContentText("不可以删除工厂设备");
                alert.showAndWait();
            }else if(equipMsgEntity.getRentState().equals("已被租用")){
                alert.setHeaderText("!");
                alert.setContentText("设备已被租用，不可以删除哦");
                alert.showAndWait();
            }else if(equipMsgEntity.getRentState().equals("未被租用")){
                equipMsgDao.deleteMsg(equipMsgEntity.getId());
            }

        }


        equipMsgEntities = new ArrayList<>();
        this.initialize();
    }

    @FXML
    void newEquip(ActionEvent event) throws IOException {
        myApp.goToNewEquipPage();
        this.initialize();
        equipMsgEntities = new ArrayList<>() ;

    }

    @FXML
    void returnAllInfo(ActionEvent event) throws IOException {
        this.displayInfo(equipMsgDao.findMsgList());
        equipMsgEntities = new ArrayList<>() ;

    }

    @FXML
    void returnMenu(ActionEvent event) throws Exception {
        myApp.goToAdminLoginPage();
    }

    @FXML
    void searchEquip(ActionEvent event) throws IOException {
        String str = searchName.getText();
        List<EquipMsgEntity> equips = equipMsgDao.findMsgByName(str);
        this.displayInfo(equips);
        equipMsgEntities = new ArrayList<>() ;

    }
    private void addButtonToTable(){
        Callback<TableColumn<EquipMsgProperty, Void>, TableCell<EquipMsgProperty, Void>> cellFactory = new Callback<TableColumn<EquipMsgProperty, Void>, TableCell<EquipMsgProperty, Void>>() {
            @Override
            public TableCell<EquipMsgProperty, Void> call(final TableColumn<EquipMsgProperty, Void> param) {
                final TableCell<EquipMsgProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            EquipMsgProperty equipMsgProperty = getTableView().getItems().get(getIndex());
                            try {
                                equipMsgEntities.add(equipMsgDao.findMsg(equipMsgProperty.getId()));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        option.setCellFactory(cellFactory);

    }
    void displayInfo(List<EquipMsgEntity> equipMsgEntityList){
        equipTable.getItems();
        ObservableList<EquipMsgProperty> equipMsgProperties = FXCollections.observableArrayList();
        if(equipMsgEntityList!=null){
            for(EquipMsgEntity equipMsgEntity:equipMsgEntityList){
                EquipMsgProperty equipMsgProperty = new EquipMsgProperty();
                equipMsgProperty.setId(equipMsgEntity.getId());
                equipMsgProperty.setDetail(equipMsgEntity.getDetail());
                equipMsgProperty.setMode(equipMsgEntity.getMode());
                equipMsgProperty.setType(equipMsgEntity.getType());
                equipMsgProperty.setName(equipMsgEntity.getName());
                equipMsgProperty.setCode(equipMsgEntity.getCode());
                equipMsgProperty.setFactory(equipMsgEntity.getFactory());
                equipMsgProperty.setEquipState(equipMsgEntity.getEquipState());
                equipMsgProperty.setRentState(equipMsgEntity.getRentState());

                equipMsgProperties.add(equipMsgProperty);
            }
            addButtonToTable();

            id.setCellValueFactory(param -> param.getValue().idProperty());
            detail.setCellValueFactory(param -> param.getValue().detailProperty());
            mode.setCellValueFactory(param -> param.getValue().modeProperty());
            type.setCellValueFactory(param -> param.getValue().typeProperty());
            name.setCellValueFactory(param -> param.getValue().nameProperty());
            code.setCellValueFactory(param -> param.getValue().codeProperty());
            factory.setCellValueFactory(param -> param.getValue().factoryProperty());
            equipState.setCellValueFactory(param -> param.getValue().equipStateProperty());
            rentState.setCellValueFactory(param -> param.getValue().rentStateProperty());

            equipTable.setItems(equipMsgProperties);
        }else{
            equipTable.setItems(FXCollections.emptyObservableList());
        }

    }

}
